<?php
require('../includes/class.main.php');
$manager = new MeetingManager();
require_once('../includes/class.search.php');

require_once('../includes/class.meeting.php');

header('Content-type: application/json');
$params = new SearchParams();

$params->searchType = (isset($_GET['searchType'])) ? $_GET['searchType'] : "exact";

$params->location  = (isset($_GET['location'])) ? $_GET['location'] : "";
$params->address = isset($_GET['address']) ? trim($_GET['address']) : "";
$params->city = isset($_GET['city']) ? trim($_GET['city']) : "";
$params->zip = (isset($_GET['zip'])) ? $_GET['zip'] : "";
$params->lat = (isset($_GET['lat'])) ? $_GET['lat'] : "";
$params->lng = (isset($_GET['lng'])) ? $_GET['lng'] : "";
$params->radius = (isset($_GET['radius'])) ? $_GET['radius'] : "";

$params->day = isset($_GET['day']) ? trim($_GET['day']) : "";

if (isset($_GET['endTime'])) {
	$params->endTime = trim($_GET['endTime']);
	if ($params->endTime) {
		$s = "";
		if (isset($_GET['meridianEnd'])) {
			$s = trim($_GET['meridianEnd']);
		}
		if (!$s) {
			$s = "PM";
		}
		$params->endTime .= " " . $s;
	}	
}

if (isset($_GET['startTime'])) {
	$params->startTime = trim($_GET['startTime']);
	if ($params->startTime) {
		$s = "";
		if (isset($_GET['meridianStart'])) {
			$s = trim($_GET['meridianStart']);
		}
		if (!$s) {
			$s = "AM";
		}
		$params->startTime .= " " . $s;
	}
}

$params->group = (isset($_GET['group'])) ? $_GET['group'] : ""; 
$params->codes = isset($_GET['codes']) ? $_GET['codes'] : null;

$return = array();
$search = new MeetingSearch();
$status = $search->searchMeetings($params, $results, $sort);
if ($status) { 
	$return['status'] = "ERR";
	$return['message'] = $status;

} else { 
	$return['status'] = "OK";
	$return['sort'] = $sort;
	$return['results'] = $results;
}

echo json_encode($return);
?>